import json
from fastapi import Depends, APIRouter
from pydantic import BaseModel

from routes.slot_management import sql_polars,sqlpyodbc
from util.JWT import get_current_user


query_slot = APIRouter()

# 刷新
class queryType(BaseModel):
    con: str
    flag: int

@query_slot.post("/getquery")
async def getquery(data:queryType,id: dict = Depends(get_current_user)):
    sql=""
    if data.flag==0:
        sql=f"SELECT ContractNo,CaseNo,LocationID FROM CaseResult WHERE  ContractNo ='{data.con}' ORDER BY ContractNo,CaseNo"
    else:
        sql=f"SELECT ContractNo,CaseNo,LocationID FROM CaseResult WHERE  ContractNo IN({data.con}) ORDER BY ContractNo,CaseNo"
    df = sql_polars.query(sql)
    if df.height < 1:
        return {
            "code": 204,
            "status": "no",
            "msg": "没有内容",
            "data": []
        }
    df = df.to_dicts()
    json_string = json.dumps(df, ensure_ascii=False)
    return {
        "code": 200,
        "status": "ok",
        "msg": "success",
        "data": json_string
    }

# 出库